package io.bio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 多线程支持
 */
public class BioServer1 {
    static ExecutorService executorService = Executors.newCachedThreadPool();
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8080);
        System.out.println("服务器已启动，监听8080端口");
        while(true) {
            final Socket request = serverSocket.accept();
            executorService.execute(() -> {
                System.out.println("建立连接，" + request.toString());
                BufferedReader bufferedReader = null;
                try {
                    bufferedReader = new BufferedReader(new InputStreamReader(request.getInputStream()));
                    String str;
                    while ((str = bufferedReader.readLine()) != null)
                        System.out.println("收到数据：" + str);
                    System.out.println("数据接收完毕，数据来自：" + request.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });

        }
    }
}
